<?php

namespace App\Listeners;

use App\Events\CourseEvent;
use App\Events\Event;
use App\Events\AccountEvent;
use App\Model\Business\Classe;
use App\Model\Marketing\Order;
use App\Model\Business\ClasseStuden;

class OrderListener extends Listener
{

    /**
     * 处理
     * @param CourseEvent $event
     */
    public function handle(CourseEvent $event)
    {
        switch($event->action)
        {
            case Event::STUDENT:
                $this->student($event); break;
            default: break;
        }
    }

    /**
     *
     * @param $event
     */
    protected function student($event)
    {
        $identity = $event->identity;
        $students = $event->students;
        $oldStudents = $event->oldStudents;

        # 获取班级信息
        $classe = Classe::find($event->getClasseId());

        # 正式学生，先把移除的学生，修改订单状态
        if($identity == ClasseStuden::STUENDT){
            Order::wherein('studentid', $oldStudents)
                ->where('productid', $classe->courseuid)
                ->where('status', Order::ASSIGNED)
                ->update(['status'=> Order::PAID]);

            Order::wherein('studentid', $students)
                ->where('productid', $classe->courseuid)
                ->where('status', Order::PAID)
                ->update(['status'=>Order::ASSIGNED]);
        }
    }



}
